﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FirstGame
{
    
    public class MainMenuScreen : MenuScreen
    {
        ButtonItem Newgame, Loadgame, Setting, Exit, Help;
        Frame _MainFrame;

        VolumeControl VolumeSoundEffect, VolumeSoundMusic;
        CheckItem Easy, Normal, Hard;
        ButtonItem OkOfSettingFrame;
        Frame _SettingFrame;
        
        
        public MainMenuScreen(ContentManager content, ScreenManager screenmanager, string background, string[] strTextures1, string[] strTextures2, Vector2 topleft)
        {
            TopLeft = topleft;
            Screenmanager = screenmanager;
            Background = content.Load<Texture2D>(background);


            Items = new List<MenuItem>();
            int nItems = strTextures1.Length;

            //_MainFrame
            Newgame = new ButtonItem(content, this, strTextures1[0], strTextures2[0], new Vector2(TopLeft.X, TopLeft.Y),
                                            new Vector2(-150, TopLeft.Y), new Vector2(150, 70));
            Newgame.RecColis = new Rectangle(5, 15, (int)(Newgame.Size.X - 20), (int)(Newgame.Size.Y - 30));
            Newgame.ParentFrame = _MainFrame;
            Newgame.Selected += new EventHandler(Newgame_Selected);
            Items.Add(Newgame);
            Loadgame = new ButtonItem(content, this, strTextures1[1], strTextures2[1], new Vector2(TopLeft.X, TopLeft.Y + 60),
                                            Newgame.SetRelativePosition(), new Vector2(150, 70));
            Loadgame.RecColis = new Rectangle(5, 15, (int)(Loadgame.Size.X - 20), (int)(Loadgame.Size.Y - 30));
            Loadgame.ParentFrame = _MainFrame;
            Loadgame.Selected += new EventHandler(Continue_Selected);
            Items.Add(Loadgame);
            Setting = new ButtonItem(content, this, strTextures1[2], strTextures2[2], new Vector2(TopLeft.X, TopLeft.Y + 120),
                                            Loadgame.SetRelativePosition(), new Vector2(150, 70));
            Setting.RecColis = new Rectangle(5, 15, (int)(Setting.Size.X - 20), (int)(Setting.Size.Y - 30));
            Setting.ParentFrame = _MainFrame;
            Setting.Selected += new EventHandler(Setting_Selected);
            Setting.Focused += new EventHandler(Setting_Focused);
            Items.Add(Setting);

            Help = new ButtonItem(content, this, strTextures1[3], strTextures2[3], new Vector2(TopLeft.X, TopLeft.Y + 180),
                                            Setting.SetRelativePosition(), new Vector2(150, 70));
            Help.RecColis = new Rectangle(5, 15, (int)(Help.Size.X - 20), (int)(Help.Size.Y - 30));
            Help.ParentFrame = _MainFrame;
           
            Items.Add(Help);

            

            Exit = new ButtonItem(content, this, strTextures1[4], strTextures2[4], new Vector2(TopLeft.X, TopLeft.Y + 240),
                                            Help.SetRelativePosition(), new Vector2(150, 70));
            Exit.RecColis = new Rectangle(5, 15, (int)(Exit.Size.X - 20), (int)(Exit.Size.Y - 30));
            Exit.Selected += new EventHandler(Exit_Selected);
            Exit.ParentFrame = _MainFrame;
            Items.Add(Exit);
            Exit.Exited += new EventHandler(Exit_Exited);


            Newgame.MoveTime = TimeSpan.FromSeconds(0.25);
            Loadgame.MoveTime = TimeSpan.FromSeconds(0.25);
            Setting.MoveTime = TimeSpan.FromSeconds(0.25);
            Help.MoveTime = TimeSpan.FromSeconds(0.25);
            Exit.MoveTime = TimeSpan.FromSeconds(0.25);

            Newgame.ChangeDirection();            
            Loadgame.ChangeDirection();
            Setting.ChangeDirection();
            Help.ChangeDirection();
            Exit.ChangeDirection();

            this.Entered += new EventHandler(MainMenuScreen_Entered);

            _CoverTexture = content.Load<Texture2D>("BackgroundTexture\\nen");
            
            _MainFrame = new Frame(content, "TextureMenuItems\\menu01", Vector2.Zero, new Vector2(0, -470), new Vector2(200, 470));
            _MainFrame.Entered += new EventHandler(_MainFrame_Entered);
            _MainFrame.Exited += new EventHandler(_MainFrame_Exited);
            this.Exited += new EventHandler(MainMenuScreen_Exited);
            _MainFrame.MoveTime = TimeSpan.FromSeconds(0.4);
            // ket thuc _mainframe va cac item

            // khoi tao cho setting
            string[] strTextureIncre = { "TextureMenuItems\\incre1", "TextureMenuItems\\incre2" };
            string[] strTextureDecre = { "TextureMenuItems\\decre1", "TextureMenuItems\\decre2" };
            VolumeSoundEffect = new VolumeControl(content, this, strTextureIncre, strTextureDecre, "TextureMenuItems\\Standing", "TextureMenuItems\\Animate1", 
                                                new Vector2(300,150), new Vector2(300,-250));
            VolumeSoundMusic = new VolumeControl(content, this, strTextureIncre, strTextureDecre, "TextureMenuItems\\Standing", "TextureMenuItems\\Animate1", 
                                                new Vector2(300, 190), new Vector2(300, -210));
            Easy = new CheckItem(content, this, "TextureMenuItems\\unselect", "TextureMenuItems\\select",
                                new Vector2(300,275), new Vector2(300,-125), new Vector2(20,20));
            Easy.ParentFrame = _SettingFrame;
            Normal = new CheckItem(content, this, "TextureMenuItems\\unselect", "TextureMenuItems\\select",
                                    new Vector2(410, 275), new Vector2(410, -125), new Vector2(20, 20));
            Normal.ParentFrame = _SettingFrame;
            Hard = new CheckItem(content, this, "TextureMenuItems\\unselect", "TextureMenuItems\\select",
                                new Vector2(520, 275), new Vector2(520, -125), new Vector2(20, 20));
            Hard.ParentFrame = _SettingFrame;

            OkOfSettingFrame = new ButtonItem(content, this, "TextureMenuItems\\bt_oke", "TextureMenuItems\\bt_oke_selection", 
                                                new Vector2(360,295), new Vector2(360,-105), new Vector2(150, 50));
            OkOfSettingFrame.RecColis = new Rectangle(5, 10, (int)(OkOfSettingFrame.Size.X - 20), (int)(OkOfSettingFrame.Size.Y - 20));
            OkOfSettingFrame.Selected += new EventHandler(OkOfSettingFrame_Selected);
            //OkOfSettingFrame.MoveTime = TimeSpan.FromSeconds(5);
            OkOfSettingFrame.ChangeDirection();

            Items.Add(VolumeSoundEffect);
            Items.Add(VolumeSoundMusic);
            Items.Add(Easy);
            Items.Add(Normal);
            Items.Add(Hard);
            Items.Add(OkOfSettingFrame);
            _SettingFrame = new Frame(content, "TextureMenuItems\\settingmenu", new Vector2(200, 50), new Vector2(200, -350), new Vector2(450, 350));
            _SettingFrame.Exited += new EventHandler(_SettingFrame_Exited);
            

            NItems = Items.Count;
           

             
        }

        void _SettingFrame_Exited(object sender, EventArgs e)
        {
            //_MainFrame.Move();
        }

        void OkOfSettingFrame_Selected(object sender, EventArgs e)
        {
            MoveSettingFrameAndItem();

            // IsAppear MainFrame;
            _MainFrame.Move();
        }

        void MoveSettingFrameAndItem()
        {
            _SettingFrame.Move();
            VolumeSoundEffect.Move();
            VolumeSoundMusic.Move();
            Easy.Move();
            Normal.Move();
            Hard.Move();
            OkOfSettingFrame.Move();          
        }


        void MainMenuScreen_Exited(object sender, EventArgs e)
        {
            Screenmanager.Introscreen.Active();
        }

        void _MainFrame_Exited(object sender, EventArgs e)
        {
            /*
            _SettingFrame.Move();            
            Easy.Move();
            Normal.Move();
            Hard.Move();
            VolumeSoundEffect.Move();
            VolumeSoundMusic.Move();
            OkOfSettingFrame.Move();
             * */
        }

        void _MainFrame_Entered(object sender, EventArgs e)
        {
            MoveMainItem();
        }

        void Exit_Exited(object sender, EventArgs e)
        {
            _MainFrame.Move();
        } 

        void MainMenuScreen_Entered(object sender, EventArgs e)
        {

            _MainFrame.Move();
        }

        void Setting_Focused(object sender, EventArgs e)
        {
            
        }

        void Setting_Selected(object sender, EventArgs e)
        {
            MoveMainItem();
            MoveSettingFrameAndItem();
            //this.Cancel();    
            
        }

        private void MoveMainItem()
        {
            Newgame.Move();
            Loadgame.Move();
            Setting.Move();
            Help.Move();
            Exit.Move();
        }

        void Exit_Selected(object sender, EventArgs e)
        {
            
        }

        

        void Continue_Selected(object sender, EventArgs e)
        {
            
        }

        void Newgame_Selected(object sender, EventArgs e)
        {
           
        }


        public override void Update(GameTime gameTime, InputSystem input)
        {
            if (_MainFrame != null)
            {
                _MainFrame.Update(gameTime, input);
            }
            if (_SettingFrame != null)
            {
                _SettingFrame.Update(gameTime, input);
            }

            
           
            for (int i = 0; i < _nItems; i++)
            {                
                Items[i].Update(gameTime, input);
            }        
               
           

            
           
            
            
            base.Update(gameTime, input);

        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(Background, new Rectangle(0, 0,
                                           800, 480),
                                            new Color(255, 255, 255, 255));
            if (_MainFrame != null)
                _MainFrame.Draw(gameTime, spriteBatch);
            if (_SettingFrame != null)
                _SettingFrame.Draw(gameTime, spriteBatch);
            VolumeSoundEffect.Draw(gameTime, spriteBatch);
            VolumeSoundMusic.Draw(gameTime, spriteBatch);
            
            for (int i = 0; i < _nItems; i++)
            {
                Items[i].Draw(gameTime, spriteBatch);
            }

            

            spriteBatch.Draw(_CoverTexture, new Rectangle(0, 0, 800, 600), new Color(255, 255, 255, 255 - ScreenAlpha));


        }
    }
}
